import type { Router, LocationQueryRaw } from 'vue-router'

import { useUserStore } from '@/store'
import { isLogin } from '@/utils/common/auth'

export default function useLoginGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    console.log(to, 'to')

    if (isLogin()) {
      console.log('success')
      next()
    } else {
      /**
       * 未登入执行逻辑
       */

      //当前跳转路径为登入页则放行
      if (to.path === '/cvmagic/login') {
        next()
        return
      }

      //重定向到登入页面
      next({
        path: '/cvmagic/login',
        query: {
          redirect: to.name,
          ...to.query,
        } as LocationQueryRaw,
      })
    }
  })
}
